package com.acttime.GenericLib;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WebDriverCommonUtil {
	// Boolean flag
	boolean flag = false;
	//(1) wait for page to load
	public void waitForPageToLoad()
	{
		Driver.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	}
	//(2) Enter Values in Text Field
	public void enterValuesInField(String xpathElement, String details)
	{
		Driver.driver.findElement(By.name(xpathElement)).sendKeys(details);
	}
	//(3) Click on Button By ID
	public void clickById(String idElement) {
		Driver.driver.findElement(By.id(idElement)).click();
	}
	//(4) Click on Button By Name
	public void clickByName(String nameElement) {
		Driver.driver.findElement(By.name(nameElement)).click();
	}
	//(5) Click on Button By Xpath
	public void clickByXpath(String xpathElement) {
		Driver.driver.findElement(By.xpath(xpathElement)).click();
	}
	//(6) wait for element by xpath
	public void waitForElementXpathPresent(String xpath)
	{
		WebDriverWait wait = new WebDriverWait(Driver.driver, 10);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
	}
	//(7) wait for element by name
	public void waitForElementNamePresent(String name)
	{
		WebDriverWait wait = new WebDriverWait(Driver.driver, 10);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.name(name)));
	}
	//(8) wait for element by name
	public void waitForElementIdPresent(String id)
	{
		WebDriverWait wait = new WebDriverWait(Driver.driver, 10);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.name(id)));
	}
	//(9) Verify Text present-can be part of text also
	public boolean verifyTextPresent(String expectedText)
	{
		boolean flag = false;
		String entrirePageSource = Driver.driver.getPageSource();
		if(entrirePageSource.contains(expectedText))
		{
			flag = true;
		}
		return flag;
		
	}
	//(10) accept alert
	public String acceptAlert()
	{
		//Switch to Alert
		Alert alert = Driver.driver.switchTo().alert();
		// Accept alert
		alert.accept();
		// Get Alert text
		String alertText = alert.getText();
		return alertText;
	}
	//(11) cancel alert
	public String cancelAlert()
	{
		//Switch to Alert
		Alert alert = Driver.driver.switchTo().alert();
		// Cancel/dismiss alert
		alert.dismiss();
		// Get Alert text
		String alertText = alert.getText();
		return alertText;
	}
	//(12) get Alert Text
	public String getAlertText()
	{
		//Switch to Alert
		Alert alert = Driver.driver.switchTo().alert();
		// Get Alert text
		String alertText = alert.getText();
		return alertText;
	}
	//(13) Click and Get Row Data
	public String[] clickAndGetRowData(String rowXpath,String clickableElement)
	{
		String[] rowData = null;
		boolean flag = true;
		rowXpath = "" + rowXpath + "";
		while(flag)
		{
			try{
				waitForPageToLoad();
				Driver.driver.findElement(By.linkText(rowXpath)).click();
				waitForPageToLoad();
				String verifyCustomerName = Driver.driver.findElement(By.xpath("//td[@class='i']/descendant::td/span")).getText();
				rowData = verifyCustomerName.split(" ");
				break;
			}catch(Exception e){
				
			//Click on next if enabled	
			boolean nextIsEnabled= Driver.driver.findElement(By.linkText("Next")).isEnabled();
			//if(Driver.driver.findElement(By.linkText(clickableElement)).isEnabled())
			if(nextIsEnabled)
			{
				Driver.driver.findElement(By.linkText(clickableElement)).click();
				flag = true;
			}else
			{
				flag = false;
			}
			}
		}
		return rowData;
		
		}
	//(14) get Row data from table
	public String[] getRowData()
	{
		String[] rowData = null;
		try{
			String entireRowData = Driver.driver.findElement(By.xpath("//form[@id='customersProjectsForm']/table/tbody/tr/td/table/tbody/tr[6]/td/table/tbody/tr/td[1]/table/tbody/tr/td")).getText();
			rowData = entireRowData.split(" ");
		}catch(Exception e)
		{
			System.out.println("Row not found");	
		}
		return rowData;
	}
	// GetWindow id
	public void getWindowId()
	{
		
	}
}
